<template>
	<div class="system-edit-dept-container">
		<el-dialog :title="(ruleForm.deptId !== 0 ? '修改' : '添加') + '部门'" v-model="isShowDialog" width="769px">
			<el-form ref="formRef" :model="ruleForm" :rules="rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20" v-if="index === '' || index !== 0">
						<el-form-item label="上级部门" prop="parentId">
							<el-cascader
								:options="deptTree"
								:props="{ checkStrictly: true, emitPath: false, value: 'deptId', label: 'deptName' }"
								placeholder="请选择部门"
								class="w100"
								v-model="ruleForm.parentId"
							>
								<template #default="{ node, data }">
									<span>{{ data.deptName }}</span>
									<span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
								</template>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门名称" prop="deptName">
							<el-input v-model="ruleForm.deptName" placeholder="请输入部门名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="负责人">
							<el-input v-model="ruleForm.leader" placeholder="请输入负责人" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号" prop="phone">
							<el-input v-model="ruleForm.phone" placeholder="请输入手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="邮箱" prop="email">
							<el-input v-model="ruleForm.email" placeholder="请输入" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number v-model="ruleForm.orderNum" :min="0" :max="999" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="部门状态">
							<el-switch
								v-model="ruleForm.status"
								:active-value="1"
								:inactive-value="0"
								inline-prompt
								active-text="启"
								inactive-text="禁"
							></el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ ruleForm.deptId !== 0 ? '修 改' : '添 加' }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, toRefs, unref } from 'vue';
import { addDept, editDept, getDeptSelectTree } from '/@/api/system/dept';
import { ElMessage } from 'element-plus';

// 定义接口来定义对象的类型
interface TableDataRow {
	id: number;
	parentId: number;
	deptName: string;
	children?: TableDataRow[];
}

interface RuleFormState {
	deptId: number;
	parentId: number;
	ancestors: string;
	deptName: string;
	orderNum: number;
	leader: string;
	phone: string | number;
	email: string;
	status: number;
}

interface DeptSate {
	isShowDialog: boolean;
	ruleForm: RuleFormState;
	deptTree: Array<TableDataRow>;
	index: string | number;
	rules: object;
}

export default defineComponent({
	name: 'systemDeptEdit',
	setup(prop, { emit }) {
		const formRef = ref<HTMLElement | null>(null);
		const validateParentId = (rule: any, value: any, callback: any) => {
			if (value === 0) {
				callback(new Error('上级部门不能为空'));
			} else {
				callback();
			}
		};
		const state = reactive<DeptSate>({
			isShowDialog: false,
			ruleForm: {
				deptId: 0,
				parentId: 0, // 上级部门
				ancestors: '',
				deptName: '', // 部门名称
				orderNum: 0,
				leader: '',
				phone: '',
				email: '',
				status: 1,
			},
			deptTree: [], // 部门数据
			index: '',
			rules: {
				parentId: [
					{ required: true, message: '上级部门不能为空', trigger: 'blur' },
					{ validator: validateParentId, trigger: 'blur' },
				],
				deptName: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
				email: [
					{
						type: 'email',
						message: "'请输入正确的邮箱地址",
						trigger: ['blur', 'change'],
					},
				],
				mobile: [
					{
						pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
						message: '请输入正确的手机号码',
						trigger: 'blur',
					},
				],
			},
		});
		// 打开弹窗
		const openDialog = (index: number, row?: RuleFormState | number) => {
			resetForm();
			getDeptSelectTree().then((res: any) => {
				state.deptTree = res.data.deptTree;
			});
			if (row && typeof row === 'object') {
				state.ruleForm = row;
				state.index = index;
			} else if (row && typeof row === 'number') {
				state.ruleForm.parentId = row;
				state.index = '';
			}
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = () => {
			state.isShowDialog = false;
			emit('deptList');
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			const formWrap = unref(formRef) as any;
			if (!formWrap) return;
			formWrap.validate((valid: boolean) => {
				if (valid) {
					if (state.ruleForm.deptId === 0) {
						//添加
						addDept(state.ruleForm).then(() => {
							ElMessage.success('部门添加成功');
							closeDialog(); // 关闭弹窗
						});
					} else {
						//修改
						editDept(state.ruleForm).then(() => {
							ElMessage.success('部门修改成功');
							closeDialog(); // 关闭弹窗
						});
					}
				}
			});
		};
		const resetForm = () => {
			state.ruleForm = {
				deptId: 0,
				parentId: 0, // 上级部门
				ancestors: '',
				deptName: '', // 部门名称
				orderNum: 0,
				leader: '',
				phone: '',
				email: '',
				status: 1,
			};
			state.index = '';
		};
		return {
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			formRef,
			...toRefs(state),
		};
	},
});
</script>
